package com.qfedu.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

public class DruidUtil {
    private static DataSource dataSource;
    //对数据源进行初始化
    static {
        //创建配置文件对象
        Properties properties=new Properties();
        //获取配置文件的输入流
        InputStream rcsourceAsStream=DruidUtil.class.getClassLoader().getResourceAsStream("Druid.properties");
        //加载配置文件
        try {
            properties.load(rcsourceAsStream);
            //给数据源赋值
            dataSource=DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //获取连接
    public Connection getConnection(){
        Connection connection=null;
        try {
            connection = dataSource.getConnection();
        } catch (Exception e){

        }
        return connection;
    }

    //获取数据源方法
    public static DataSource getDataSource() {
        return dataSource;
    }
}
